package Presentation.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import Domain.BookList;
import Data.BookListMapper;

import Presentationlayout.AdminSearchBar;

/**
 * Listener that executes a search for books in the database and present the
 * result in a {@link Presentationlayout.AdminSearchResult} TableModel.
 * The listener is used for the search button of the 
 * {@link Presentationlayout.AdminSearchBar} object that is part of the
 * {@link Presentationlayout.Admin} object.
 * 
 * @author Rodrigo Acuna
 *
 */

public class SearchAction implements ActionListener {

	private AdminSearchBar adminSearchBar;
	
	/**
	 * Constructor that takes the {@link Presentationlayout.AdminSearchBar} 
	 * object that is the client of this listener.
	 * 
	 * @param adminSearchBar
	 *            the {@link Presentationlayout.AdminSearchBar} object
	 */
	public SearchAction(AdminSearchBar adminSearchBar) {
		this.adminSearchBar = adminSearchBar;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		System.out.println("actionPerformed called");
		BookList bookList = new BookList();
		BookListMapper listMapper = new BookListMapper();
		
		String searchString = adminSearchBar.getFreeSearch();
		
		try {

			bookList = listMapper.searchFor(searchString);
			adminSearchBar.setBookList(bookList);
			// view for the search results
			adminSearchBar.getAdmin().searchResult();
			// add the search results to the JPanel
			adminSearchBar.revalidate();
			// adminSearchBar.repaint();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//AdminSearchResult.createResultTableModel(bookList);
		System.out.println(bookList + "\n");
		

	}

}
